package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.WipMarkingScreenMapper;
import com.xdcplus.web.common.pojo.entity.WipMarkingScreen;
import com.xdcplus.web.common.pojo.dto.WipMarkingScreenDTO;
import com.xdcplus.web.common.pojo.dto.WipMarkingScreenFilterDTO;
import com.xdcplus.web.common.pojo.vo.WipMarkingScreenVO;
import com.xdcplus.web.common.pojo.query.WipMarkingScreenQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingScreenService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * 质量结批报告-网板浆料统计(WipMarkingScreen)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-11-29 14:52:56
 */
@Slf4j
@Service("wipMarkingScreenService")
public class WipMarkingScreenServiceImpl extends BaseServiceImpl<WipMarkingScreenMapper, WipMarkingScreen, WipMarkingScreen, WipMarkingScreenVO> implements WipMarkingScreenService {

    @Autowired
    protected WipMarkingScreenMapper wipMarkingScreenMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingScreen(WipMarkingScreenDTO wipMarkingScreenDTO) {

        WipMarkingScreen wipMarkingScreen = new WipMarkingScreen();
        BeanUtil.copyProperties(wipMarkingScreenDTO, wipMarkingScreen);
        wipMarkingScreen.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(wipMarkingScreen);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingScreen(WipMarkingScreenDTO wipMarkingScreenDTO) {

        WipMarkingScreen wipMarkingScreen = this.getById(wipMarkingScreenDTO.getId());
        if (ObjectUtil.isNull(wipMarkingScreen)) {
            log.error("updateWipMarkingScreen() The WipMarkingScreen does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingScreenDTO, wipMarkingScreen);
        wipMarkingScreen.setUpdatedUser(wipMarkingScreenDTO.getUpdatedUser());
        wipMarkingScreen.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingScreen);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingScreen> wipMarkingScreenList) {

        wipMarkingScreenList.forEach(wipMarkingScreen -> {
            WipMarkingScreen wipMarkingScreenParam = new WipMarkingScreen();
            wipMarkingScreenParam.setId(wipMarkingScreen.getId());
            if (ObjectUtil.isNotNull(wipMarkingScreen.getId())) {
                wipMarkingScreen.setId(wipMarkingScreen.getId());
                wipMarkingScreen.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingScreen> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingScreen::getId, wipMarkingScreen.getId());
                update(wipMarkingScreen, lambdaUpdate);
            } else {
                wipMarkingScreen.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingScreen);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingScreenDTO> wipMarkingScreenDTOList) {

        List<WipMarkingScreen> wipMarkingScreenList = BeanUtils.copyProperties(wipMarkingScreenDTOList, WipMarkingScreen.class);
        return saveOrUpdateBatch(wipMarkingScreenList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingScreenLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingScreen wipMarkingScreen = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingScreen)) {
            log.error("deleteWipMarkingScreen() The WipMarkingScreen does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingScreenByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingScreen wipMarkingScreen = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingScreen)) {
				log.error("deleteWipMarkingScreen() The WipMarkingScreen does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingScreen> queryWipMarkingScreenList(WipMarkingScreenFilterDTO wipMarkingScreenFilterDTO) {
        WipMarkingScreenQuery wipMarkingScreenQuery = BeanUtil.copyProperties(wipMarkingScreenFilterDTO, WipMarkingScreenQuery.class);

        return wipMarkingScreenMapper.queryWipMarkingScreen(wipMarkingScreenQuery);
    }

    @Override
    public List<WipMarkingScreenVO> queryWipMarkingScreenVOList(WipMarkingScreenFilterDTO wipMarkingScreenFilterDTO) {
        return this.objectConversion(queryWipMarkingScreenList(wipMarkingScreenFilterDTO));
    }

    @Override
    public PageVO<WipMarkingScreenVO> queryWipMarkingScreen(WipMarkingScreenFilterDTO wipMarkingScreenFilterDTO) {
        PageVO<WipMarkingScreenVO> pageVO = new PageVO<>();

        if (wipMarkingScreenFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingScreenFilterDTO.getCurrentPage(), wipMarkingScreenFilterDTO.getPageSize(),
                    wipMarkingScreenFilterDTO.getOrderType(), wipMarkingScreenFilterDTO.getOrderField());
        }

        List<WipMarkingScreen> wipMarkingScreenList = queryWipMarkingScreenList(wipMarkingScreenFilterDTO);

        PageInfo<WipMarkingScreen> pageInfo = new PageInfo<>(wipMarkingScreenList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingScreenList));

        return pageVO;
    }

    @Override
    public WipMarkingScreenVO queryWipMarkingScreenById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<WipMarkingScreenVO> queryByMarkingId(Long markingId) {
        return this.objectConversion(wipMarkingScreenMapper.queryByMarkingId(markingId));
    }
}
